﻿using Microsoft.Office.Interop.Excel;

namespace ExcelTools
{
  // ==================================================================================
  /// <summary>
  /// This type represents an Excel workbook.
  /// </summary>
  /// <remarks>
  /// This type is a wrapper class around the Workbook class in the Excel PIA.
  /// </remarks>
  // ==================================================================================
  public sealed class ExcelWorkbook
  {
    #region Private fields;

    private readonly Workbook _Workbook;
    private ExcelWorksheetCollection _Worksheets;

    #endregion

    #region Lifecycle methods

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Creates a wrapper for an Excel workbook.
    /// </summary>
    /// <param name="workbook">Excel PIA Workbook instance.</param>
    // --------------------------------------------------------------------------------
    public ExcelWorkbook(Workbook workbook)
    {
      _Workbook = workbook;
    }

    #endregion

    #region Internal properties

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the Microsoft.Office.Tools.Excel.Workbook object behind this instance.
    /// </summary>
    // --------------------------------------------------------------------------------
    public Workbook Workbook
    {
      get { return _Workbook; }
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets or sets a value that indicates whether labels can be used in worksheet 
    /// formulas.
    /// </summary>
    /// <value>
    /// <b>true</b> if labels can be used in worksheet formulas. The default value 
    /// is <b>false</b>.
    /// </value>
    // --------------------------------------------------------------------------------
    public bool AcceptLabelsInFormulas
    {
      get { return _Workbook.AcceptLabelsInFormulas; }
      set { _Workbook.AcceptLabelsInFormulas = value; }
    }

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the active sheet of this workbook.
    /// </summary>
    // --------------------------------------------------------------------------------
    public ExcelWorksheet ActiveSheet
    {
      get
      {
        object activeSheet = _Workbook.ActiveSheet;
        return new ExcelWorksheet(this, activeSheet as Worksheet);
      }
    }

    // TODO: Implement ActiveChart property 
    // TODO: Implement ActiveSheet property 
    // TODO: Implement Application property 
    // TODO: Implement Author property 
    // TODO: Implement AutoUpdateFrequency property 
    // TODO: Implement AutoUpdateSaveChanges property 
    // TODO: Implement BuiltinDocumentProperties property 
    // TODO: Implement CalculationVersion property 
    // TODO: Implement ChangeHistoryDuration property 
    // TODO: Implement Charts property 
    // TODO: Implement CheckCompatibility property 
    // TODO: Implement ActiveChart property 
    // TODO: Implement Colors property 
    // TODO: Implement CommandBars property 
    // TODO: Implement Comments property 
    // TODO: Implement ConflictResolution property 
    // TODO: Implement Connections property 
    // TODO: Implement ConnectionsDisabled property 
    // TODO: Implement other properties 

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the name of the workbook, including its path on disk.
    /// </summary>
    /// <value>
    /// The name of the workbook, including its path on disk.
    /// </value>
    /// <remarks>
    /// This property is equivalent to the Path property, followed by the current file 
    /// system separator, followed by the Name property.
    /// </remarks>
    // --------------------------------------------------------------------------------
    public string FullName
    {
      get { return _Workbook.FullName; }  
    }

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the name of the workbook.
    /// </summary>
    /// <value>
    /// The name of the workbook.
    /// </value>
    // --------------------------------------------------------------------------------
    public string Name
    {
      get { return _Workbook.Name; }
    }

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the complete path to the application, excluding the final separator and 
    /// name of the application.
    /// </summary>
    /// <value>
    /// The complete path to the application, excluding the final separator and name 
    /// of the application.
    /// </value>
    // --------------------------------------------------------------------------------
    public string Path
    {
      get { return _Workbook.Path;  }
    }

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets an ExcelWorksheetCollection that represents all the worksheets in the 
    /// workbook.
    /// </summary>
    /// <value>
    /// An ExcelWorksheetCollection that represents all the worksheets in the workbook.
    /// </value>
    // --------------------------------------------------------------------------------
    public ExcelWorksheetCollection Worksheets
    {
      get 
      {
        if (_Worksheets == null)
        {
          _Worksheets = new ExcelWorksheetCollection(this);
        }
        return _Worksheets;
      }
    }
    #endregion
  }
}